package com.team5101.web.controller;



import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.team5101.dto.TProductDto;
import com.team5101.entity.TProduct;
import com.team5101.entity.TProductDesc;
import com.team5101.product.api.IProductService;
import com.team5101.util.StringUtil;
import com.team5101.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private FastFileStorageClient client;

    @Reference
    public IProductService iProductService;


    @Value("${imageServer}")
    private String imageServer;

    @RequestMapping({"/getAll",""})
    public String getInfo(Model model){
        List<TProductDto> all = iProductService.getAll();
        model.addAttribute("product",all);
        return "index";
    }

    @RequestMapping("/showPage/{pageNum}/{pageSize}")
    public String showPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize, Model model){
        ResultBean r  = iProductService.getProByPage(pageNum,pageSize);
        PageInfo<TProductDto> pageInfo =(PageInfo<TProductDto>)r.getData();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("product",pageInfo.getList());
        return "index";
    }

    @RequestMapping("/upload")
    @ResponseBody
    public ResultBean upload(MultipartFile dropzFile) throws Exception{

        InputStream inputStream = dropzFile.getInputStream();
        StorePath storePath = client.uploadImageAndCrtThumbImage(inputStream, dropzFile.getSize(), StringUtil.getFileExtName(dropzFile.getOriginalFilename()), null);
        String realPath = storePath.getFullPath();
        String imgPath = StringUtil.getStringUtil(imageServer, realPath);
        inputStream.close();
        ResultBean r = new ResultBean();
        r.setErrno(0);
        r.setData(new String[]{imgPath});
        return r;

    }
    @RequestMapping("/delPro")
    @ResponseBody
    public ResultBean delPro(Long pid){
        int i = iProductService.deleteByPrimaryKey(pid);
        if(i==1){
            return ResultBean.success("删除成功");
        }
        return ResultBean.error("删除失败");
    }

    @RequestMapping("/addPro")
    public String add(TProductDto tProductDto){
      ResultBean resultBean = iProductService.addProduct(tProductDto);

        return "redirect:showPage/1/3";

    }

    @RequestMapping("/updatePro")
    @ResponseBody
    public ResultBean updatePro(Long pid){
        TProductDto tProduct = iProductService.selectOne(pid);
        if(tProduct!=null){
            return ResultBean.success(tProduct,"查询成功");
        }
        return ResultBean.error("错误");
    }

    @RequestMapping("/EditPro")
    public String editPro(TProductDto tProductDto){
        TProduct t = new TProduct();
        t.setPid(tProductDto.getPid());
        t.setPname(tProductDto.getPname());
        t.setPrice(tProductDto.getPrice());
        t.setSalePrice(tProductDto.getSalePrice());
        t.setPimage(tProductDto.getPimage());
        t.setUpdateTime(new Date());
        int i = iProductService.updateByPrimaryKeySelective(t);
        TProductDesc td = new TProductDesc();
        td.setPid(tProductDto.getPid());
        td.setUpdateTime(new Date());
        td.setPdesc(tProductDto.getPdesc());
       int i1 = iProductService.updateByPid(td);
        return "redirect:showPage/1/3";
    }



}
